package com.ebelter.ty.ui.fragment.scale.others;


import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.ebelter.btcomlib.bases.fragment.BaseFragment;
import com.ebelter.btcomlib.common.Constants;
import com.ebelter.btcomlib.models.beans.ScaleUser;
import com.ebelter.btcomlib.models.eventbus.CommonEventBus;
import com.ebelter.btcomlib.models.https.IHttpRequestField;
import com.ebelter.btcomlib.models.https.NetUtils;
import com.ebelter.btcomlib.models.https.interfaces.HttpResponse;
import com.ebelter.btcomlib.utils.ToastUtil;
import com.ebelter.btcomlib.utils.UserSpUtil;
import com.ebelter.btcomlib.utils.log.LogUtils;
import com.ebelter.ty.R;
import com.ebelter.ty.model.http.TyNetUtil;
import com.ebelter.ty.model.http.bean.OriginalParametersResponse;
import com.ebelter.ty.ui.fragment.scale.Scale_M_B5_Fragment;

import org.greenrobot.eventbus.EventBus;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import static com.ebelter.btcomlib.utils.CommonUtils.getAgeByBirthday;
import static com.ebelter.btcomlib.utils.CommonUtils.getBirthdayIntValue;



public class Scale_M_NAME_Fragment extends BaseFragment implements View.OnClickListener {


    public static final String TAG = "Scale_M_NAME_Fragment";


    private ImageView modify_back_bt;
    private EditText modify_ed;
    private TextView modify_done_tv;

    @Override
    protected int setLayoutResouceId() {
        return R.layout.scale_modify_ly;
    }

    @Override
    protected void initView() {
        super.initView();
        FV();
    }

    @Override
    protected void initData(Bundle arguments) {
        super.initData(arguments);

    }

    private void FV() {
        modify_back_bt = mRootView.findViewById(R.id.modify_back_bt);
        modify_ed = mRootView.findViewById(R.id.modify_ed);
        modify_ed.setHint(R.string.nike_name);
        modify_ed.setMaxEms(20);
        String name = UserSpUtil.readString(Constants.IUser.NICK_NAME);
        modify_ed.setText(name);

        modify_done_tv = mRootView.findViewById(R.id.modify_done_tv);

        modify_back_bt.setOnClickListener(this);
        modify_done_tv.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.modify_back_bt:
                getActivity().finish();
                break;
            case R.id.modify_done_tv:
                modifyName();
                break;

        }
    }

    private void modifyName() {


        String modifyStr = modify_ed.getText().toString().trim();

        if (modifyStr.length()>20){
            ToastUtil.show(R.string.nikename_over_tip);
            return;
        }

        final Map<String,Serializable> paramMap=new HashMap<>();
        paramMap.put("userId", UserSpUtil.readLong(Constants.IUser.USER_ID));
        paramMap.put("nickName",modifyStr);

        if (!NetUtils.available()){
            ToastUtil.show(R.string.Network_error);
            return;
        }

        TyNetUtil.getInstance().updateInfo(this, paramMap, new HttpResponse<OriginalParametersResponse>() {
            @Override
            public void result(boolean isRequestSuccess, String requestDes, OriginalParametersResponse originalParametersResponse,String s) {
                LogUtils.i(TAG,"当前线程："+Thread.currentThread().getName());
                if (isRequestSuccess){

                    if (originalParametersResponse.resultCode==0){
                        ToastUtil.show(R.string.Modify_Success);
                        updateLocalData((Map<String, Serializable>) originalParametersResponse.originalParameters);

                        CommonEventBus eventBus=new CommonEventBus(TAG, Scale_M_B5_Fragment.TAG,0,null);
                        EventBus.getDefault().post(eventBus);

                        if (getActivity()!=null)getActivity().finish();

                    }else if (originalParametersResponse.resultCode==112){
                        ToastUtil.show(R.string.nickname_have_been_registered);
                    }else if (originalParametersResponse.resultCode==110){
                        ToastUtil.show(R.string.An_exception_occurred_on_the_server);
                    }else {
                        ToastUtil.show(R.string.Modify_Failed);
                    }

                }else {
                    ToastUtil.show(R.string.Network_connection_error);
                }
            }
        });



    }

    //更新本地数据
    public static void updateLocalData(Map<String, Serializable> originalParameters) {
        if (originalParameters==null){
            return;
        }
        Set<String> sets = originalParameters.keySet();
        if (sets.size()<=0){
            return;
        }
        for (String k:sets){
            if (TextUtils.isEmpty(k)){
                continue;
            }
            Serializable serializable = originalParameters.get(k);
            if (TextUtils.equals(IHttpRequestField.nickName,k)){
                //说明更改了昵称
                UserSpUtil.writeString(Constants.IUser.NICK_NAME, (String) serializable);
            }
            if (TextUtils.equals("height",k)){
                //说明更改了身高
                UserSpUtil.writeInt(Constants.IUser.HEIGHT, (int) serializable);
                ScaleUser.getUser().setHeight((int) serializable);
            }
            if (TextUtils.equals("birthday",k)){
                //说明更改了身高
                UserSpUtil.writeString(Constants.IUser.BIRTHDAY, (String) serializable);

                int year=getBirthdayIntValue((String) serializable,0,1990);
                UserSpUtil.writeInt(Constants.IUser.YEAR,year);


                int mon=getBirthdayIntValue((String) serializable,1,6);
                UserSpUtil.writeInt(Constants.IUser.MONTHS,mon);


                int day=getBirthdayIntValue((String) serializable,2,1);
                UserSpUtil.writeInt(Constants.IUser.DAY,day);


                int age = getAgeByBirthday(year);
                UserSpUtil.writeInt(Constants.IUser.AGE,age);
                ScaleUser.getUser().setAge(age);

            }
        }
    }
}
